﻿@model IEnumerable<FERW.Models.Task>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<form method="get" class="form-inline text-center" style="margin:10px">
    所属项目：@Html.DropDownList("ProjectID", null, new { @class = "form-control" })
    任务内容：<input type="text" name="content" class="form-control" placeholder="请输入内容关键字" />
    任务状态：<input type="radio" name="TaskState" value="2" id=" r1"><label for="r1">已完成</label>

    <input type="radio" name="TaskState" value="1" id="r2"><label for="r2">未完成</label>
    <input type="radio" name="TaskState" value="0" checked="checked" id="r3"><label for="r3">全部</label>
    <input type="submit" value="查询" class="btn btn-primary" />
    @Html.ActionLink("添加", "Create", null, new { @class = "btn btn-primary" })
</form>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ProjectID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskPriority)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AddTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskState)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FinishedTime)
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Project.ProjectName)
            </td>
            <td>
                @{
                    if (item.TaskPriority == 1)
                    {
                        @:高
                    }
                    else if (item.TaskPriority == 2)
                    {
                        @:中
                    }
                    else
                    {
                        @:低
                    }
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @(item.TaskState==1?"未完成":"已完成")
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FinishedTime)
            </td>
            <td class="text-right">
                @if (item.FinishedTime == null)
                {
                    <a href="#" data-des="@item.TaskID" class="btn btn-primary">完成任务</a>
                }
                <a href="#" data-del="@item.TaskID" class="btn btn-danger">删除</a>
            </td>
        </tr>
    }

</table>
@section scripts{
    <script type="text/javascript">
        $(function(){
            $('a[data-del]').click(function () {
                var $this = $(this);
                var id = $this.attr("data-del");
                if (!confirm("are you sure?删除第" + id + "行")) return;
                $.post('@Url.Content("~/Task/Delete")', { id, id }, function (res) {
                    if (res == true) {

                        $this.closest("tr").remove();
                        alert("删除成功");
                    }
                })
            })
        })
        $(function () {
            $("a[data-des]").click(function () {
                var $this = $(this);
                var id = $this.attr("data-des");
                $.post("@Url.Content("~/Task/Edit")", { id: id }, function (res) {
                    if (res==true) {
                        location.reload();
                        alert("完成任务!");
                    }
                })

            })

        })
    </script>

}